import React, { useState, useEffect } from "react";
import {
  message,
  Drawer,
  Form,
  Input,
  Button,
  Radio,
  InputNumber,
  Select,
  Spin,
} from "antd";
import { addField, upadataField, getDetail, dataDrive } from "../api";

const { Option } = Select;

const UpdateForm = (props) => {
  // 结构化数据
  const { visible, onCancel, onSuccess, data, type, currentIndex = 0 } = props;

  // 初始化 form
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  // 详情
  const getDetailData = () => {
    const paylaod = {
      id: data.id,
    };
    setLoading(true);
    getDetail(paylaod).then((res) => {
      setLoading(false);
      if (res.code === 0) {
        form.setFieldsValue({ ...res.data, ...data });
      } else {
        message.error(res.msg || "获取详情失败");
      }
    });
  };

  useEffect(() => {
    if (type === "updata" && visible === true) {
      getDetailData();
    } else {
      form.resetFields();
    }
    form.setFieldsValue({ state: "1", sort: currentIndex });
  }, [visible]);

  // 栅格化
  const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  };

  /**
   * @en-US Update node
   * @zh-CN values
   *
   * @param fields
   */
  const updataData = (values) => {
    const hide = message.loading("正在添加");
    setLoading(true);
    upadataField({ ...values, id: data.id }).then((res) => {
      hide();
      setLoading(false);
      if (res.code === 0) {
        onCancel(false);
        onSuccess();
      } else {
        message.error(res.msg || "修改失败，请重试");
      }
    });
  };
  /**
   * @en-US Update node
   * @zh-CN 添加
   *
   * @param values
   */
  const addData = (values) => {
    const hide = message.loading("正在添加");
    setLoading(true);

    addField({ ...values }).then((res) => {
      hide();
      setLoading(false);
      if (res.code === 0) {
        onCancel(false);
        onSuccess();
      } else {
        message.error(res.msg || "添加失败，请重试");
      }
    });
  };

  /**
   * @en-US Update node
   * @zh-CN 更新节点
   *
   * @param values
   */
  const sendData = () => {
    form.validateFields().then((values) => {
      if (type === "updata") updataData(values);
      if (type === "add") addData(values);
    });
  };

  const modelClose = () => {
    onCancel(false);
  };

  return (
    <>
      <Drawer
        getContainer={false}
        width={640}
        title={type === "updata" ? "修改" : "添加"}
        visible={visible}
        onClose={modelClose}
        maskClosable={false}
        footer={
          <div
            style={{
              textAlign: "right",
            }}
          >
            <Button onClick={modelClose} style={{ marginRight: 10 }}>
              取消
            </Button>
            <Button
              onClick={sendData}
              type="primary"
              loading={loading}
              disabled={loading}
            >
              保存
            </Button>
          </div>
        }
      >
        <Spin spinning={loading}>
          <Form {...formItemLayout} name="control-ref" form={form}>
            <Form.Item
              label="资源名称"
              name="name"
              rules={[{ required: true }]}
            >
              <Input maxLength={20} />
            </Form.Item>
            <Form.Item
              label="资源简称"
              name="abbreviation"
              rules={[{ required: true }]}
            >
              <Input maxLength={20} />
            </Form.Item>
            <Form.Item label="domain" name="domain">
              <Input maxLength={20} />
            </Form.Item>
            <Form.Item name="state" label="激活状态">
              <Radio.Group>
                <Radio value="0">禁用</Radio>
                <Radio value="1">激活</Radio>
              </Radio.Group>
            </Form.Item>
            <Form.Item width="xs" name="sort" label="显示顺序">
              <InputNumber min={0} max={1000} />
            </Form.Item>
            <Form.Item label="目标数据源" name="dbsourceid">
              <Input maxLength={20} />
            </Form.Item>
            <Form.Item label="备注" name="meme">
              <Input.TextArea maxLength={200} allowClear />
            </Form.Item>
          </Form>
        </Spin>
      </Drawer>
    </>
  );
};

export default UpdateForm;
